using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace HREDU.Hoang_Tuan.Classes
{
    public class ListViewColumnSorter : System.Collections.IComparer
    {

        private int ColumnToSort;
        private SortOrder OrderOfSort;
        private CaseInsensitiveComparer ObjectCompare;

        public ListViewColumnSorter()
        {
            // Initialize the column to '0'.
            ColumnToSort = 1;

            // Initialize the sort order to 'none'.
            OrderOfSort = SortOrder.Ascending;

            // Initialize the CaseInsensitiveComparer object.
            ObjectCompare = new CaseInsensitiveComparer();
        }

        public int Compare(object x, object y)
        {
            int compareResult;
            ListViewItem listviewX;
            ListViewItem listviewY;

            // Cast the objects to be compared to ListViewItem objects.
            listviewX = (ListViewItem)x;
            listviewY = (ListViewItem)y;

            // Compare the two items.
            compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

            // Calculate the correct return value based on the object
            // comparison.
            if ((OrderOfSort == SortOrder.Ascending))
            {
                // Ascending sort is selected, return typical result of
                // compare operation.
                return compareResult;
            }
            else if ((OrderOfSort == SortOrder.Descending))
            {
                // Descending sort is selected, return negative result of
                // compare operation.
                return (-compareResult);
            }
            else
            {
                // Return '0' to indicate that they are equal.
                return 0;
            }
        }

        public int SortColumn
        {

            get { return ColumnToSort; }
            set { ColumnToSort = value; }
        }

        public SortOrder Order
        {

            get { return OrderOfSort; }
            set { OrderOfSort = value; }
        }
    }
}
